$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'pro/sysversion/list',
        datatype: "json",
        colModel: [			
			{ label: 'ID', name: 'id_', width: 45, key: true,hidden:true },
			{ label: '应用名称', name: 'appName', width: 75 },
			{ label: '类型', name: 'flag', width: 40 , formatter: function(value, options, row){
				return value == 0 ? 
						'<span>demo版本</span>' : 
							'<span>release版本</span>';
			}},
			{ label: '程序类型', name: 'pType', width: 40 , formatter: function(value, options, row){
				return value == 0 ? 
						'<span>sick</span>' : 
						'<span>nurse</span>';
			}},
			{ label: '版本号', name: 'versionName', width: 45 },
			{ label: '版本标识', name: 'versionCode', width: 45 },
			{ label: '备注', name: 'content', width: 45 },
			{ label: '下载url', name: 'downloadUrl', width: 300 },
        	{ label: '时间', name: 'updateTime', width: 90 }
        ],
		viewrecords: true,
        
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
 
    new AjaxUpload('#upload', {
        action: baseURL + 'pro/sysversion/saveUpload?token=' + token,
        name: 'apkFile',
        autoSubmit:true,
        responseType:"json",
        onSubmit:function(apkFile, extension){
        
            if (!(extension && /^(apk)$/.test(extension.toLowerCase()))){
                alert('只支持apk格式文件！');
                return false;
            }
        	jQuery("#down_div").removeClass("xhide");
    		jQuery("#over_div").removeClass("xhide");
        },
        onComplete : function(apkFile, r){
        	jQuery("#down_div").addClass('xhide');
        	jQuery("#over_div").addClass('xhide');
            if(r.code == 0){
               // alert(r.url);
                $("#upload").val(r.url);
                vm.sysversion.downloadUrl = r.url;
                //vm.reload();
            }else{
                alert(r.msg);
            }
        }
    });
});

function check(){
	var str=($("#versionName").val());
	var reg =/^\d*\.\d*\.\d*$/;
	if(!reg.test(str)){
		alert("输入的版本号格式错误，请重新输入")
	      }
	   return true;
}

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			appName: null
		},
		showList: true,
		title:null,
		sysversion:{
			
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		
		queryVersion: function () {
			var flag = vm.sysversion.flag;
			if(!flag){
				return;
			}
			$.ajax({
				type: "POST",
				 url: baseURL + "pro/sysversion/query",
                contentType: "application/json",
			    data: JSON.stringify(flag),
			    success: function(r){
			    	if(r.code === 0){
						var versionCode = (r.query)+1;
						$("#versionCode").val(versionCode);
						vm.sysversion.versionCode=versionCode;
					}else{
						alert(r.msg);
					}
				}
			});
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.roleList = {};
			vm.sysversion = {appName:null, versionName:null,flag:null, status:1};
			
		},
		update: function () {
			var userId = getSelectedRow();
			if(userId == null){
				return ;
			}
			
			vm.showList = false;
            vm.title = "修改";
			
			vm.getSysversion(userId);
		},
		del: function () {
			var userIds = getSelectedRows();
			if(userIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "pro/sysversion/delete",
                    contentType: "application/json",
				    data: JSON.stringify(userIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(){
                                vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		saveOrUpdate: function () {
			var url = vm.sysversion.id_ == null ? "pro/sysversion/save" : "pro/sysversion/update";
			vm.sysversion.downloadUrl =$("#upload").val();
			vm.sysversion.time =$("#time").val();
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.sysversion),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		getSysversion: function(userId){
			$.get(baseURL + "pro/sysversion/info/"+userId, function(r){
				vm.sysversion = r.sysversion;
				vm.sysversion.passWord = null;

			});
		},
       
		reload: function () {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                postData:{'appName': vm.q.appName},
                page:page
            }).trigger("reloadGrid");
		},
		downloadData: function (url) {
			jQuery("#down_div").removeClass("xhide");
   			jQuery("#over_div").removeClass("xhide");
			
			jQuery("#down_div").addClass('xhide');
			jQuery("#over_div").addClass('xhide');
		}
	}
});